package cn.simple.model.base;

import cn.simple.annotation.Note;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Random;

/**
 * 游戏角色基类
 */
@Getter
@Setter
@AllArgsConstructor
public abstract class Role {

    @Note("角色名称")
    private String name;

    @Note("等级")
    private Integer level;

    @Note("血量")
    private Integer hp;

    @Note("魔法值")
    private Integer mp;

    @Note("力量")
    private Integer power;

    @Note("防御")
    private Integer defense;

    @Note("速度")
    private Integer speed;

    @Note("技能组")
    private List<Skill> skillList;

    @Note("是否存活")
    private Boolean isAlive = true;

    public Role() {
        //先加载默认值
        defaultInitRoleInfo(null);
        //用户自定义方法，必须实现，指定角色名称
        init();
        if (name == null || name.isEmpty()) {
            throw new RuntimeException("角色必须拥有名称");
        }
    }

    /**
     * 初始化绝角色
     */
    protected abstract void init();

    /**
     * 使用技能
     *
     * @param skill    技能
     * @param roleList 角色
     */
    protected abstract void useSkill(Skill skill, List<Role> roleList);

    /**
     * 检查目前角色是否存活
     */
    protected void checkAliveAndThenSet() {
        if (this.hp <= 0) {
            this.isAlive = false;
        }
    }

    protected void defaultInitRoleInfo(String name){
        setName(name);
        setHp(100);
        setMp(100);
        setLevel(1);
        setPower(getRandomInt(1,10));
        setDefense(getRandomInt(1,10));
        setSpeed(getRandomInt(1,10));
    }

    /**
     * 生成1-upperLimit的随机数
     *
     * @param lowerLimit 下限
     * @param upperLimit 上限
     * @return
     */
    protected int getRandomInt(int lowerLimit, int upperLimit) {
        Random random = new Random();
        return random.nextInt(upperLimit) + lowerLimit;
    }


}
